package com.bringway.p2p.dao.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;
import org.springframework.jdbc.core.namedparam.MapSqlParameterSource;
import org.springframework.jdbc.core.namedparam.SqlParameterSource;
import org.springframework.stereotype.Repository;

import com.bringway.framework.dao.BaseJdbcDao;
import com.bringway.p2p.dao.P2PRoleDao;

import net.sf.json.JSONObject;

/*
 * @author fan
 * @className P2PRoleDaoImpl
 * @description P2PRole  持久层dao实现类
 * @date 2015年12月20日下午2:48:05
 */
@Repository
public class P2PRoleDaoImpl extends BaseJdbcDao implements P2PRoleDao {
	private Logger logger=Logger.getLogger(P2PRoleDaoImpl.class);

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.bringway.p2p.dao.P2PRoleDao#getRoleList(net.sf.json.JSONObject)
	 */
	@Override
	public List<Map<String, Object>> getRoleList(JSONObject pamrs) {
		String parent_id = pamrs.optString("parent_id");
		JSONObject parmsMap = new JSONObject();
		StringBuffer sql = new StringBuffer();
		sql.append("select * from t_fm_sys_role where PARENT_ID = :parent_id ");
		parmsMap.put("parent_id", parent_id);
		logger.info("getRoleList sql is "+sql.toString());
		logger.info("getRoleList parmsMap is "+parmsMap);
		return this.getNamedParameterJdbcTemplate().queryForList(sql.toString(), parmsMap);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.bringway.p2p.dao.P2PRoleDao#addRole(net.sf.json.JSONObject)
	 */
	@Override
	public int addRole(JSONObject parms) {
		String id = parms.optString("id");
		String name = parms.optString("name");
		String parent_id = parms.optString("parent_id");
		StringBuffer sql = new StringBuffer();
		sql.append("INSERT INTO `t_fm_sys_role`(`ID`,`NAME`,`PARENT_ID`) values(:id,:name,:parent_id) ");
		SqlParameterSource parmsMap = new MapSqlParameterSource().addValue("id", id).addValue("name", name)
				.addValue("parent_id", parent_id);
		return this.getNamedParameterJdbcTemplate().update(sql.toString(), parmsMap);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.bringway.p2p.dao.P2PRoleDao#delRole(net.sf.json.JSONObject)
	 */
	@Override
	public int delRole(JSONObject parms) {
		String id = parms.optString("id");
		StringBuffer sql = new StringBuffer();
		sql.append(" DELETE FROM `d_ccb_fm`.`t_fm_sys_role` WHERE `ID` = :id  ");
		SqlParameterSource parmsMap = new MapSqlParameterSource().addValue("id", id);
		return this.getNamedParameterJdbcTemplate().update(sql.toString(), parmsMap);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.bringway.p2p.dao.P2PRoleDao#updateRole(net.sf.json.JSONObject)
	 */
	@Override
	public boolean updateRole(JSONObject parms) {
		String id = parms.optString("id");
		String name = parms.optString("name");
		String parent_id = parms.optString("parent_id");
		StringBuffer sql = new StringBuffer();
		sql.append(
				" UPDATE `d_ccb_fm`.`t_fm_sys_role` SET `ID` = :id, `NAME`=:name, `PARENT_ID`= :parent_id WHERE `ID`= :id ");
		SqlParameterSource parmsMap = new MapSqlParameterSource().addValue("id", id).addValue("name", name)
				.addValue("parent_id", parent_id);
		return this.getNamedParameterJdbcTemplate().update(sql.toString(), parmsMap)>0;
	}
	
	/*
	 * (non-Javadoc)
	 * @see com.bringway.p2p.dao.P2PRoleDao#getRoleList()
	 */
	@Override
	public List<Map<String, Object>> getRoleList() {
		JSONObject parmsMap = new JSONObject();
		StringBuffer sql = new StringBuffer();
		sql.append(" select * from t_fm_sys_role ");
		return this.getNamedParameterJdbcTemplate().queryForList(sql.toString(), parmsMap);
	}

	@Override
	public int delMenuRole(JSONObject parms) {
		String roleId = parms.optString("id");
		StringBuffer sql = new StringBuffer();
		sql.append(" delete from d_ccb_fm.t_fm_sys_menu_role where ROLE_ID = :roleId ");
		Map<String,Object> paramsMap = new HashMap<String,Object>();
		paramsMap.put("roleId",roleId);
		return this.getNamedParameterJdbcTemplate().update(sql.toString(), paramsMap);
	}

	@Override
	public int delUserRole(JSONObject parms) {
		String roleId= parms.optString("id");
		StringBuffer sql = new StringBuffer();
		sql.append(" delete from d_ccb_fm.t_fm_sys_user_role where ROLE_ID = :roleId ");
		Map<String,Object> paramsMap = new HashMap<String,Object>();
		paramsMap.put("roleId",roleId);
		return this.getNamedParameterJdbcTemplate().update(sql.toString(), paramsMap);
	}

}
